# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from ui_compoundEdit import Ui_CompoundEditFrame
from MySQLdb import OperationalError

class CompoundEdit(QtGui.QFrame):
	def __init__(self, controller, compoundClass, mode='New', readonly=False):
		QtGui.QFrame.__init__(self)

		self.controller = controller

		self.compoundClass = compoundClass
		self.compound = None

        # Set up the user interface from Designer.
		self.ui = Ui_CompoundEditFrame()
		self.ui.setupUi(self)

		self.readonly = readonly
		self.setReadonly(self.readonly)

		self.setMode(mode)

		self.connect(self.ui.viewStructurePushButton, QtCore.SIGNAL("clicked()"), self.controller.viewStructure)
		self.connect(self.ui.addCompoundPushButton, QtCore.SIGNAL("clicked()"), self.insertCompound)
		self.connect(self.ui.updatePushButton, QtCore.SIGNAL("clicked()"), self.updateCompound)
		self.connect(self.ui.searchPushButton, QtCore.SIGNAL("clicked()"), self.search)
		self.connect(self.ui.productsPushButton, QtCore.SIGNAL("clicked()"), self.searchProducts)
		self.connect(self.ui.reactantsPushButton, QtCore.SIGNAL("clicked()"), self.searchReactants)


	def clear(self):
		self.ui.structureTextEdit.clear()

	def setStructure(self, string):
		if string:
			self.ui.structureTextEdit.clear()
			self.ui.structureTextEdit.insertPlainText(string)

	def getStructureText(self):
		return self.ui.structureTextEdit.toPlainText()

	def insertCompound(self):
		self.compound = self.compoundClass()

		self.compound.insert()

		self.updateCompound()
		self.ui.idLineEdit.setText(str(self.compound.id))

	def updateCompound(self):
		self.compound.name = self.ui.nameLineEdit.text()
		self.compound.formula = self.ui.formulaLineEdit.text()
		self.compound.IUPACname = self.ui.IUPACLineEdit.text()
		self.compound.inchi.inchi = self.ui.inchiLineEdit.text()

		self.compound.mol.molfile = self.getStructureText()

	def search(self):
		self.compound = self.compoundClass()
		self.compound.name = self.ui.nameLineEdit.text()
		self.compound.formula = self.ui.formulaLineEdit.text()
		self.compound.IUPACname = self.ui.IUPACLineEdit.text()
		self.compound.inchi.inchi = self.ui.inchiLineEdit.text()
		if self.mode != "Dialog":
			self.controller.searchCompound(self.compound)
		else:
			self.controller.searchCompoundFromDialog(self.compound)

	def setCurrentCompound(self, compound, readonly=False):
		self.compound = compound
		self.updateFields()
		self.setReadonly(readonly)

	def updateFields(self):
		self.setStructure(self.compound.mol.molfile)

		self.ui.nameLineEdit.setText(self.compound.name)
		self.ui.idLineEdit.setText(str(self.compound.id))
		self.ui.formulaLineEdit.setText(self.compound.formula)
		self.ui.IUPACLineEdit.setText(self.compound.IUPACname)
		if self.compound.inchi.inchi:
			self.ui.inchiLineEdit.setText(self.compound.inchi.inchi)

	def setReadonly(self, readonly):
		self.readonly = readonly

		self.ui.nameLineEdit.setReadOnly(readonly)
		self.ui.formulaLineEdit.setReadOnly(readonly)
		self.ui.IUPACLineEdit.setReadOnly(readonly)
		self.ui.inchiLineEdit.setReadOnly(readonly)
		self.ui.structureTextEdit.setReadOnly(readonly)

	def searchProducts(self):
		print "searching products"
		self.controller.addCompoundsToSearch(self.compound.products, "Products")

	def searchReactants(self):
		print "searching reactants"
		self.controller.addCompoundsToSearch(self.compound.reactants, "Reactants")

	def setMode(self, mode):
		self.mode = mode
		if mode == "New":
			self.ui.searchPushButton.hide()
			self.ui.updatePushButton.hide()
			self.ui.addCompoundPushButton.show()
		if mode == "Search":
			self.ui.searchPushButton.show()
			self.ui.updatePushButton.show()
			self.ui.addCompoundPushButton.hide()
		if mode == "Dialog":
			self.ui.searchPushButton.show()
			self.ui.updatePushButton.hide()
			self.ui.addCompoundPushButton.hide()
